<?php
@include_once('../includes/config.inc');
if ($_POST['create_playlist']) {
   ?>
   <br />
   <a href="<?=$HTTP_DIR?>outgoing/current.m3u" >Click here to new open the playlist.</a>
   <br />
   <?php
   $db = "playlist_gen";

   $music_table = 'mp3';
   $event_table = 'psa';
   
   set_time_limit(0);
   mysql_connect("localhost","root","kaurkaur")
       or die(mysql_error());
   mysql_select_db($db)
       or die(mysql_error());
   
   $hour = date('H');
   $minute = date('i') + $_POST['delay'];
   $second = date('s');
   
   $last_play = 0;
   
   $filedata = '#EXTM3U' . "\n";
   $playlength = (((($hour * 60) + $minute) * 60) + $second);
   $max_playlength = ((3600 * $_POST['max_playlength']) + $playlength);
   
   //echo(' current seconds<br />' . $current_seconds . '<br />' . ($current_seconds + $max_playlength) . '<br />');
   
   while ($playlength < $max_playlength) {
      $rand_track_sql = mysql_query("SELECT * FROM `$music_table` ORDER BY RAND() LIMIT 1 ");
      $track = mysql_fetch_array($rand_track_sql);
      $temp_time = explode(':', $track['playtime']);
      
      if (count($temp_time) == 2) {	// this will execute if the track time appears as xx:xx only -- thus, it'll only add tracks to the playlist if they are more at least a minute and less than an hour long.
         $time = (($temp_time[0] * 60) + $temp_time[1]);
	   
         $filedata .= '#EXTINF:';
         $filedata .= ($time . ', ' . $track['artist'] . ' - ' . $track['title'] . "\n");
         $filedata .= ($track['filepath'] . '/' . $track['filename'] . "\n");
         $playlength += $time;
         //$filedata .= 
         $mlapse = ((($playlength - ($playlength % 60)) / 60) %60);  //retrieve the minute past the hour
         
         if (($last_play + 780) < $playlength) {
            if (($mlapse >= 57) || ($mlapse < 05) || ($mlapse < 24 && $mlapse >17) || ($mlapse > 37 && $mlapse < 44)) {
               $filedata .= '#EXTINF:';
               $rand_event_sql = mysql_query("SELECT * FROM `$event_table` ORDER BY RAND() LIMIT 1 ");
               $event = mysql_fetch_array($rand_event_sql);
               $temp_time = explode(':', $event['playtime']);
               if (count($temp_time) == 2) {
                  $event_time = (($temp_time[0] * 60) + $temp_time[1]);
               } else if (count($temp_time) == 1) {
                  $event_time = $temp_time;
               }
               $filedata .= ($event_time . ', ' . $event['artist'] . ' - ' . $event['title'] . "\n" . $event['filepath'] . '/' . $event['filename'] . "\n");
               $last_play = $playlength;
               $playlength += $event_time;
            }
         }
      }
   }
}

$filename = ($ROOT_DIR . 'outgoing/current.m3u');

if (is_writable($filename)) {
   if (!$handle = fopen($filename, 'w+')) {
      echo "Cannot open file ($filename)";
      exit;
   }
   if (fwrite($handle, $filedata) === FALSE) {
      echo "Cannot write to file ($filename)";
      exit;
   }
//   echo "Success, wrote to file ($filename)";
   fclose($handle);
} else {
   echo "The file $filename is not writable";
}
   ?><pre><?php /* echo($filedata) */ ?></pre><?php
} else {
   ?>
   <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
   <fieldset>
   <legend>Playlist Options</legend>
   Start playlist in <select name="delay"><option value="15" selected="selected">15 Seconds</option><option value="30">30 Seconds</option><option value="60">1 Minute</option><option value="90">1.5 Minutes</option><option value="120">2 Minutes</option><option value="180">3 Minutes</option><option value="240">4 Minutes</option><option value="300">5 Minutes</option><option value="360">6 Minutes</option></select><br />
   Playlist length: <select name="max_playlength"><?php
   for ($i = 1; $i < 30; $i++){
      echo('<option value="' . $i . '"');
      if ($i == 16) echo (' selected="selected"');
      echo('>' . $i . ' hours</option>');
   }
   ?></select><br />
   <input type="hidden" name="mode" value="m3umake" />
   <input type="hidden" name="cat" value="<?=$cat?>" />
   <input type="hidden" name="create_playlist" value="true" />
   <input type="submit" value="Generate Playlist &gt;&gt;" />
   </fieldset>
   </form>
   <?php
}
?><br /><small>(<a href="../index.php">back to login</a>).</small>